package com.example.big_upload.controller;


import com.example.big_upload.model.RespBean;
import com.example.big_upload.service.BigUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {

    @Autowired
    BigUploadService bigUploadService;

    /**
     * 分片上传第一步 检查该文件是否上传
     * @param token 唯一标识
     * @param total 分片总数
     */
    @PostMapping("/checkMerge")
    public RespBean checkMerge(String token, Integer total){
        return bigUploadService.checkMerge(token,total);
    }

    /**
     * 上传分片文件
     * @param file 分片文件
     * @param token 分片文件的唯一标识，用来校验分片文件完整性
     * @param number 分片序号
     * @param parentToken 合并文件唯一标识，用来获取文件路径
     * @param fileName 合并文件名
     */
    @PostMapping("/uploadPart")
    public RespBean uploadPart(@RequestParam("file")MultipartFile file,
                               @RequestParam("token")String token,
                               @RequestParam("number")Integer number,
                               @RequestParam("parentToken")String parentToken,
                               @RequestParam("fileName") String fileName){
        return bigUploadService.uploadPart(file, token, number, parentToken,fileName);
    }

    /**
     *
     * @param token 合并文件的唯一标识
     * @param fileName 合并的文件名
     */
    @PostMapping("/merge")
    public RespBean merge(String token,String fileName){
        return bigUploadService.merge(token,fileName);
    }

    /**
     * 直接上传
     * @param file 文件
     * @param token 唯一标识
     */
    @PostMapping("/upload")
    public RespBean upload(MultipartFile file,String token){
        return bigUploadService.upload(file,token);
    }

}
